﻿Public Class App
    Dim _settings As AppSettings
    Dim _usingMySettingFile As Boolean
    WithEvents _splash As frmSplash
    Dim _rev As String = "0000.00.001"
    Dim _provider As String = "AboveGround by Jamnut.net"
    Dim _release As String = "beta - 01"
    Dim _taskDelay As Integer = 500
    WithEvents _uimanager As uiManager
    Dim _AppContext As ApplicationContext
    Dim _useAsync As Boolean = True


    Public ReadOnly Property startUpIsCanceled() As Boolean
        Get
            If _splash Is Nothing Then
                Return False
            Else
                Return _splash.IsCanceled
            End If
        End Get
    End Property

    Public Sub New()
        _AppContext = New ApplicationContext
    End Sub
    Public Function GetApplicationContext() As ApplicationContext
        StartApplication()
        Return _AppContext
    End Function
    Public Sub StartApplication()
        _splash = New frmSplash(_provider, _rev, _release)
        _splash.Show()
        SendText("Welcome to the plaesure dome...")
        StartAsync()
    End Sub
    Private Async Sub StartAsync()
        Dim completed As Task(Of String) = StartupAppAsync()
        FinishStartUp(Await completed)
    End Sub
    Private Async Function StartupAppAsync() As Task(Of String)
        Await Task.Delay(_taskDelay)
        Dim message As String = Await RunStartcodeAsync(0)
        SendText(message)
        Await Task.Delay(_taskDelay)
        If Not startUpIsCanceled Then
            SendText("Finished Startup.")
        Else
            message = "Start was Canceled."
            CancelStartUp()
        End If
        Return message
    End Function
    Private Sub FinishStartUp(ByVal message As String)
        If String.IsNullOrEmpty(message) Then
            SendText("Startup completed successfully.")
            _AppContext.MainForm = StartUpUI()
            _AppContext.MainForm.Show()
        Else
            SendText("Startup completed with exception - " & message)
            _AppContext.ExitThread()
        End If
        System.Threading.Thread.Sleep(700)
        _splash.Visible = False
        _splash.Close()
        _splash = Nothing
    End Sub
    Private Async Function RunStartcodeAsync(ByVal dwell As Integer) As Task(Of String)
        Dim message As String = String.Empty
        SendText("Initializing StartUp.")
        Try
            Await SetupAppSettings()
            SendText(Await StubForWork(1))
            SendText(Await StubForWork(2))
            SendText(Await StubForWork(3))
            SendText(Await StubForWork(4))
        Catch ex As Exception
            message = ex.Message
        End Try
        Return message
    End Function
    Private Sub SendText(ByVal text As String)
        _splash.SetText(text)
    End Sub
    Public Function StartUpUI() As Form
        _uimanager = New uiManager
        Return _uimanager.Getui
    End Function
    Private Function CheckForSettingsFile() As String
        Dim filename As String = String.Empty
        If String.IsNullOrEmpty(My.Settings.SettingFile) Then
            Dim PersonalFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
            filename = PersonalFolder & "\t4mfgDefault.settings"
        Else
            filename = My.Settings.SettingFile
            _usingMySettingFile = True
        End If
        Return filename
    End Function
    Private Async Function SetupAppSettings() As Task(Of Boolean)
        Dim message As String = "Settings from file - "
        Await Task.Delay(_taskDelay)
        If Not startUpIsCanceled Then
            Dim filename As String = CheckForSettingsFile()
            If String.IsNullOrEmpty(filename) Then
                message = "Settings file - " & filename & " does not exist."
            Else
                message = "Setting file - " & filename & " was used."
            End If
            SendText(message)
            _settings = AppSettings.Instance()
            _settings.ReadSettingsFromXML(filename)
        End If
    End Function
    Private Async Function StubForWork(ByVal int As Integer) As Task(Of String)
        Dim message As String = String.Empty
        If Not startUpIsCanceled Then
            SendText("Entered StubforWork - " & int.ToString)
            For i As Integer = 0 To 10
                If startUpIsCanceled Then
                    SendText("Canceled in StubforWork - " & int.ToString)
                    Exit For
                End If
                Await Task.Delay(100)
            Next
            message = "Finished work in StubForWork - " & int.ToString
        End If
        Return message
    End Function
    Private Sub CancelStartUp()
        If _uimanager IsNot Nothing Then
            _uimanager.CloseUI()
        End If
        SendText("Application Startup was Canceled.")
    End Sub
End Class
